package server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 封装Response对象，依赖OutputStream
 */
public class Response {

    private OutputStream outputStream;

    public Response(){

    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void output(String content) throws IOException {
        outputStream.write(content.getBytes());
    }

    /**
     * @param path url,随后要根据url来获取到静态资源的绝对路径，进一步根据绝对路径读取该静态资源文件，最终通过输出流输出
     */
    public void outputHtml(String path) throws IOException {
//        String absoulteResoucePath = StaticResourceUtil.getAbsolutePath(path);

        File file = new File(path);
        if(file.exists() && file.isFile()){
            // 读取静态资源文件，输出静态资源
            StaticResourceUtil.outputStaticResouce(new FileInputStream(file), outputStream);
        }else{
            //输出404
            output(HttpProtocolUtil.getHttpHeader404());
        }
    }

}
